package service;

import bean.AddCenter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dao.RoleDao;
import domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService {
    @Autowired
    private RoleDao roleDao;

    //查询所有：新增admin时返回至/admin/getAllRoles()方法
    public List<Role> findAllRole(){
        return roleDao.findAll();
    }

    //分页查询所有
    public PageInfo<Role> findAll(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Role> all = roleDao.findAll();
        return  new PageInfo<>(all);
    }

    //新增角色
    public void addRole(Role role,int[] permissionId){
        roleDao.add(role);
        AddCenter addCenter = new AddCenter();
        int id = role.getId();
        for (int per:permissionId){
            addCenter.setNum1(id);
            addCenter.setNum2(per);
            roleDao.addRole_Permission(addCenter);
        }
    }

    //根据id查询
    public Role findRoleById(int id){
        return roleDao.findById(id);
    }

    //修改role信息
    public void updateRole(Role role,int[] permissionId){
        roleDao.update(role);
        roleDao.deleteRole_Permission(role.getId());
        AddCenter addCenter = new AddCenter();
        for (int id:permissionId){
            addCenter.setNum1(role.getId());
            addCenter.setNum2(id);
            roleDao.addRole_Permission(addCenter);
        }
    }
}
